package org.elephantt.webby;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;

/**
 * A simple http application that uses WebbyServletRequest and WebbyServletResponse. Applications that want a custom
 * request/response class should extend HttpApplication directly.
 *
 * @see org.elephantt.webby.HttpApplication
 */
public abstract class SimpleHttpApplication extends HttpApplication {
  public SimpleHttpApplication() {
    this(new HttpApplicationConfig());
  }

  public SimpleHttpApplication(HttpApplicationConfig config) {
    super(config);
  }

  public Dispatcher createDispatcher() {
    Dispatcher dispatcher = super.createDispatcher();
    dispatcher.registerResponder(FtlView.class, createFtlResponder());
    return dispatcher;
  }

  /**
   * Configures FreeMarker
   */
  public FtlViewResponder createFtlResponder() {
    Configuration configuration = new Configuration();
    configuration.setObjectWrapper(new DefaultObjectWrapper());
    configuration.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
    configuration.setClassForTemplateLoading(getClass(), "/template");
    return new FtlViewResponder(configuration, "text/html");
  }

  public RequestResponseFactory<Request, Response> createRequestResponseFactory() {
    return new SimpleRequestResponseFactory();
  }
}
